package it.stasbranger.myproject.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;

import com.google.inject.Inject;

import it.stasbranger.myproject.client.managed.request.ApplicationEntityTypesProcessor;
import it.stasbranger.myproject.client.managed.request.ApplicationRequestFactory;
import it.stasbranger.myproject.client.managed.request.RequestsProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersHasRequestsProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersProxy;

import it.stasbranger.myproject.client.managed.ui.RequestsListView;
import it.stasbranger.myproject.client.managed.ui.RequestsMobileListView;
import it.stasbranger.myproject.client.managed.ui.SuppliersHasRequestsListView;
import it.stasbranger.myproject.client.managed.ui.SuppliersHasRequestsMobileListView;
import it.stasbranger.myproject.client.managed.ui.SuppliersListView;
import it.stasbranger.myproject.client.managed.ui.SuppliersMobileListView;

import it.stasbranger.myproject.client.scaffold.ScaffoldApp;

import it.stasbranger.myproject.client.scaffold.place.ProxyListPlace;

/**
 * Instantiates master activities.
 */
public final class ApplicationMasterActivities implements ActivityMapper {
	private final ApplicationRequestFactory requests;
	private final PlaceController placeController;

	@Inject
	public ApplicationMasterActivities(ApplicationRequestFactory requests, PlaceController placeController) {
		this.requests = requests;
		this.placeController = placeController;
	}

	public Activity getActivity(Place place) {

		if (!(place instanceof it.stasbranger.myproject.client.scaffold.place.ProxyListPlace)) {
			return null;
		}

		ProxyListPlace listPlace = (ProxyListPlace) place;

		return new ApplicationEntityTypesProcessor<Activity>() {

			@Override
			public void handleSuppliers(SuppliersProxy isNull) {
				setResult(new SuppliersListActivity(requests, ScaffoldApp.isMobile() ? SuppliersMobileListView.instance() : SuppliersListView.instance(), placeController));
			}

			@Override
			public void handleRequests(RequestsProxy isNull) {
				setResult(new RequestsListActivity(requests, ScaffoldApp.isMobile() ? RequestsMobileListView.instance() : RequestsListView.instance(), placeController));
			}

			@Override
			public void handleSuppliersHasRequests(SuppliersHasRequestsProxy isNull) {
				setResult(new SuppliersHasRequestsListActivity(requests, ScaffoldApp.isMobile() ? SuppliersHasRequestsMobileListView.instance() : SuppliersHasRequestsListView.instance(), placeController));
			}
		}.process(listPlace.getProxyClass());
	}
}
